package com.yiqi.lottery.feature.football.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiqi.lottery.R;
import com.yiqi.lottery.entity.bo.BallMatchBo;
import com.yiqi.lottery.framework.DataServer;

import java.util.ArrayList;
import java.util.List;

public class FootballAllTypeAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private List<BallMatchBo.MatchInfo> mMatchInfos;
    private int mCurrentType;

    public FootballAllTypeAdapter(Context context) {
        mContext = context;
    }

    public void setMatchInfo(List<BallMatchBo.MatchInfo> matchInfos, int currentType) {
        mMatchInfos = matchInfos;
        mCurrentType = currentType;
        notifyDataSetChanged();
    }


    @Override
    public int getGroupCount() {
        return 4;
    }

    @Override
    public int getChildrenCount(int i) {
        if (i == 0) {
            return 2;
        } else if (i == 1) {
            return 3;
        } else {
            return 1;
        }
    }

    @Override
    public Object getGroup(int i) {
        return i;
    }

    @Override
    public Object getChild(int i, int i1) {
        return i1;
    }

    @Override
    public long getGroupId(int i) {
        return 0;
    }

    @Override
    public long getChildId(int i, int i1) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean isExpanded, View view, ViewGroup viewGroup) {
        GroupViewHolder holder;
        if (view == null) {
            holder = new GroupViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_football_match_group, null);
            holder.iv_xiala = view.findViewById(R.id.iv_xiala);
            holder.tv_date_match_num = view.findViewById(R.id.tv_date_match_num);
            view.setTag(holder);
        } else {
            holder = (GroupViewHolder) view.getTag();
        }
        if (isExpanded) {
            holder.iv_xiala.setRotation(180);
        } else {
            holder.iv_xiala.setRotation(0);
        }
        if (i == 0) {
            holder.tv_date_match_num.setText("胜平负／让球");
        } else if (i == 1) {
            holder.tv_date_match_num.setText("比分");
        } else if (i == 2) {
            holder.tv_date_match_num.setText("进球数");
        } else {
            holder.tv_date_match_num.setText("半全场");
        }
        return view;
    }


    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        ChildViewHolder holder;
        if (view == null) {
            holder = new ChildViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_football_all_type, null);
            holder.tv_title = view.findViewById(R.id.tv_title);
            holder.ll1 = view.findViewById(R.id.ll1);
            holder.ll2 = view.findViewById(R.id.ll2);
            holder.ll3 = view.findViewById(R.id.ll3);
            holder.top_view = view.findViewById(R.id.top_view);
            holder.bottom_line = view.findViewById(R.id.bottom_line);
            view.setTag(holder);
        } else {
            holder = (ChildViewHolder) view.getTag();
        }

        holder.top_view.setVisibility(i1 == 0 ? View.VISIBLE : View.GONE);
        holder.bottom_line.setVisibility(i1 == getChildrenCount(i) - 1 ? View.VISIBLE : View.GONE);
        BallMatchBo.MatchInfo play;
        ArrayList<String> list = null;
        if (i == 0) {
            holder.ll1.setVisibility(View.VISIBLE);
            holder.ll2.setVisibility(View.GONE);
            holder.ll3.setVisibility(View.GONE);
            if (i1 == 0) {
                play = getMatchInfoByType("0");
                if (mCurrentType == 6) {
                    list = play.getSpTypeGg();
                } else {
                    list = play.getSpTypeDg();
                }
                setTitle(holder.tv_title, "0", Color.parseColor("#FF7BB4"), Color.parseColor("#FFDCEB"));
            } else {
                play = getMatchInfoByType("1");
                if (mCurrentType == 6) {
                    list = play.getSpTypeGg();
                } else {
                    list = play.getSpTypeDg();
                }
                int hand = Integer.parseInt(play.getHand());
                setTitle(holder.tv_title, hand > 0 ? "+" + play.getHand() : play.getHand(), Color.parseColor("#FFFFFF"), Color.parseColor("#A3E1FF"));
            }
            resetItems(i, holder.ll1, 0, list, play, DataServer.FOOT_BALL_SPF);
        } else if (i == 1) {
            play = getMatchInfoByType("4");//mMatchInfos.get(4).getPlay();
            if (mCurrentType == 6) {
                list = play.getSpTypeGg();
            } else {
                list = play.getSpTypeDg();
            }
            if (i1 == 1) {
                holder.ll1.setVisibility(View.VISIBLE);
                holder.ll2.setVisibility(View.GONE);
                holder.ll3.setVisibility(View.GONE);
                resetItems(i, holder.ll1, 13, list, play, DataServer.FOOT_BALL_BF);
            } else {
                holder.ll1.setVisibility(View.VISIBLE);
                holder.ll2.setVisibility(View.VISIBLE);
                holder.ll3.setVisibility(View.VISIBLE);
                if (i1 == 0) {
                    int index = resetItems(i, holder.ll1, 0, list, play, DataServer.FOOT_BALL_BF);
                    index = resetItems(i, holder.ll2, index, list, play, DataServer.FOOT_BALL_BF);
                    resetItems(i, holder.ll3, index, list, play, DataServer.FOOT_BALL_BF);
                } else {
                    int index = resetItems(i, holder.ll1, 18, list, play, DataServer.FOOT_BALL_BF);
                    index = resetItems(i, holder.ll2, index, list, play, DataServer.FOOT_BALL_BF);
                    resetItems(i, holder.ll3, index, list, play, DataServer.FOOT_BALL_BF);
                }
            }
            if (i1 == 0) {
                setTitle(holder.tv_title, "胜", Color.parseColor("#FF7BB4"), Color.parseColor("#FFDCEB"));
            } else if (i1 == 1) {
                setTitle(holder.tv_title, "平", Color.parseColor("#FFFFFF"), Color.parseColor("#FACD89"));
            } else {
                setTitle(holder.tv_title, "负", Color.parseColor("#FFFFFF"), Color.parseColor("#AEB0E4"));
            }
        } else if (i == 2) {
            play = getMatchInfoByType("2");
            if (mCurrentType == 6) {
                list = play.getSpTypeGg();
            } else {
                list = play.getSpTypeDg();
            }
            holder.ll1.setVisibility(View.VISIBLE);
            holder.ll2.setVisibility(View.VISIBLE);
            holder.ll3.setVisibility(View.GONE);
            setTitle(holder.tv_title, "总进球", Color.parseColor("#FF7BB4"), Color.parseColor("#FFDCEB"));
            int index = resetItems(i, holder.ll1, 0, list, play, DataServer.FOOT_BALL_ZJQ);
            resetItems(i, holder.ll2, index, list, play, DataServer.FOOT_BALL_ZJQ);
        } else {
            play = getMatchInfoByType("3");
            if (mCurrentType == 6) {
                list = play.getSpTypeGg();
            } else {
                list = play.getSpTypeDg();
            }
            holder.ll1.setVisibility(View.VISIBLE);
            holder.ll2.setVisibility(View.VISIBLE);
            holder.ll3.setVisibility(View.VISIBLE);
            setTitle(holder.tv_title, "半全场", Color.parseColor("#FF7BB4"), Color.parseColor("#FFDCEB"));
            int index = resetItems(i, holder.ll1, 0, list, play, DataServer.FOOT_BALL_BQC);
            index = resetItems(i, holder.ll2, index, list, play, DataServer.FOOT_BALL_BQC);
            resetItems(i, holder.ll3, index, list, play, DataServer.FOOT_BALL_BQC);
        }
        return view;
    }

    private BallMatchBo.MatchInfo getMatchInfoByType(String type) {
        for (BallMatchBo.MatchInfo info : mMatchInfos) {
            if (info.getType().equals(type)) {
                return info.getPlay();
            }
        }
        return new BallMatchBo.MatchInfo();
    }

    private int resetItems(int i, LinearLayout ll, int startIndex, List<String> list, BallMatchBo.MatchInfo play, String[] ballNames) {
        int childCount = ll.getChildCount();
        if (list == null) {
            list = new ArrayList<>();
        }
        LinearLayout item;
        for (int index = 0; index < childCount; ++index) {
            item = (LinearLayout) ll.getChildAt(index);
            if (i == 0) {
                if ((list.isEmpty() && index >= 1) || index >= 3) {
                    item.setVisibility(View.GONE);
                    continue;
                } else {
                    item.setVisibility(View.VISIBLE);
                }
                item.setOrientation(LinearLayout.HORIZONTAL);
            } else if (i == 3) {
                if (index >= 3) {
                    item.setVisibility(View.GONE);
                    continue;
                } else {
                    item.setVisibility(View.VISIBLE);
                }
                item.setOrientation(LinearLayout.HORIZONTAL);
            } else if (i == 2) {
                if (index >= 4) {
                    item.setVisibility(View.GONE);
                    continue;
                } else {
                    item.setVisibility(View.VISIBLE);
                }
                item.setOrientation(LinearLayout.HORIZONTAL);
            } else {
                if (ll.getId() == R.id.ll3) {
                    if (index >= 3) {
                        item.setVisibility(View.INVISIBLE);
                        continue;
                    }
                } else {
                    item.setVisibility(View.VISIBLE);
                }
                item.setOrientation(LinearLayout.VERTICAL);
            }
            TextView tv1 = ((TextView) item.getChildAt(0));
            TextView tv2 = ((TextView) item.getChildAt(2));
            if (list.isEmpty() && i == 0) {
                tv1.setText("未开售");
                tv2.setVisibility(View.GONE);
                item.getChildAt(1).setVisibility(View.GONE);
                item.setOnClickListener(null);
                continue;
            } else {
                tv2.setVisibility(View.VISIBLE);
                item.getChildAt(1).setVisibility(View.VISIBLE);
            }
            Resources resources = mContext.getResources();
            tv1.setText(ballNames[startIndex]);
            tv2.setText(list.isEmpty() ? "--" : list.get(startIndex));
            if (!list.isEmpty()) {
                item.setTag(R.id.tag, startIndex);
                item.setTag(play);
                item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int index = (int) view.getTag(R.id.tag);
                        BallMatchBo.MatchInfo play = (BallMatchBo.MatchInfo) view.getTag();
                        play.setTempCheck(index, !play.isTempCheck(index));
                        LinearLayout item = (LinearLayout) view;
                        if (play.isTempCheck(index)) {
                            item.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                            ((TextView) item.getChildAt(0)).setTextColor(resources.getColor(R.color.primary_comparison));
                            ((TextView) item.getChildAt(2)).setTextColor(resources.getColor(R.color.primary_comparison));
                        } else {
                            item.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                            ((TextView) item.getChildAt(0)).setTextColor(resources.getColor(R.color.text_primary));
                            ((TextView) item.getChildAt(2)).setTextColor(resources.getColor(R.color.text_secondary));
                        }
                    }
                });
                if (play.isTempCheck(startIndex)) {
                    item.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                    ((TextView) item.getChildAt(0)).setTextColor(resources.getColor(R.color.primary_comparison));
                    ((TextView) item.getChildAt(2)).setTextColor(resources.getColor(R.color.primary_comparison));
                } else {
                    item.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                    ((TextView) item.getChildAt(0)).setTextColor(resources.getColor(R.color.text_primary));
                    ((TextView) item.getChildAt(2)).setTextColor(resources.getColor(R.color.text_secondary));
                }
            }else{
                item.setOnClickListener(null);
                item.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                ((TextView) item.getChildAt(0)).setTextColor(resources.getColor(R.color.text_primary));
                ((TextView) item.getChildAt(2)).setTextColor(resources.getColor(R.color.text_secondary));
            }
            ++startIndex;
        }
        return startIndex;
    }

    private void setTitle(TextView tv_title, String text, int textColor, int backColor) {
        tv_title.setText(text);
        tv_title.setTextColor(textColor);
        tv_title.setBackgroundColor(backColor);
    }


    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    class GroupViewHolder {
        TextView tv_date_match_num;
        ImageView iv_xiala;
    }

    class ChildViewHolder {
        TextView tv_title;
        LinearLayout ll1, ll2, ll3;
        View top_view, bottom_line;
    }
}
